package com.xy.xymall.member.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xy.common.exception.BizCodeEnum;
import com.xy.xymall.member.entity.MemberReceiveAddressEntity;
import com.xy.xymall.member.exception.MemberException;
import com.xy.xymall.member.feign.CouponFeignService;
import com.xy.xymall.member.service.MemberReceiveAddressService;
import com.xy.xymall.member.vo.MemberLoginVo;
import com.xy.xymall.member.vo.MemberRegistVo;
import com.xy.xymall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xy.xymall.member.entity.MemberEntity;
import com.xy.xymall.member.service.MemberService;
import com.xy.common.utils.PageUtils;
import com.xy.common.utils.R;



/**
 * 会员
 *
 * @author XY
 * @email 2447315136@qq.com
 * @date 2020-04-12 09:02:50
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    CouponFeignService couponFeignService;

    /**
     * 测试feign远程调用
     */
    @RequestMapping("/coupons")
    public R memberCoupons(){
        MemberEntity memberEntity=new MemberEntity();
        memberEntity.setNickname("蛇蛇");
        R memberCoupons = couponFeignService.memberCoupons();
        Object coupons = memberCoupons.get("coupons");
        return R.ok().put("member",memberEntity).put("coupons",coupons);
    }

    @PostMapping("/oauth2/login")
    public R oauthlogin(@RequestBody SocialUser socialUser) throws Exception {
        MemberEntity memberEntity=memberService.login(socialUser);
        if(memberEntity!=null){
            return R.ok().setData(memberEntity);
        }else{
            return R.error(BizCodeEnum.USERNAME_PASSWORD_INVALID_EXCEPTION.getCode(),BizCodeEnum.USERNAME_PASSWORD_INVALID_EXCEPTION.getMessage());
        }
    }

    /**
     * @param registVo
     * @author XY
     * @date 2020/10/20 14:52
     * @return com.xy.common.utils.R
     * 注册功能
     */
    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo registVo){
        try{
            memberService.regist(registVo);
        }catch (MemberException e){
            return R.error(e.getMessage());
        }
        return R.ok();
    }

    /**
     * @param loginVo
     * @author XY
     * @date 2020/10/20 15:18
     * @return com.xy.common.utils.R
     * 用户登录功能
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo loginVo){
        MemberEntity memberEntity=memberService.login(loginVo);
        if(memberEntity!=null){
            return R.ok().setData(memberEntity);
        }else{
            return R.error(BizCodeEnum.USERNAME_PASSWORD_INVALID_EXCEPTION.getCode(),BizCodeEnum.USERNAME_PASSWORD_INVALID_EXCEPTION.getMessage());
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
